import {action, observable,toJS} from 'mobx';
import * as API from '../apis/forwardBack';
import {WeaTableNew } from 'comsMobx';
import {Modal,message} from 'antd';
import {WeaLocaleProvider, WeaTools} from 'ecCom';


const getLabel = WeaLocaleProvider.getLabel;

export default class WfForwardBackStore {

	@observable loading = false;
    @observable showForwardBack = false;
    @observable tableDatas = [];

    requestid = -1;
    callBackActions = {};
	
	@observable doBacknoSelectWarning = '';

    constructor() {
    	this.setShowForwardBack  = this.setShowForwardBack;
    	this.setCallBackAction = this.setCallBackAction;
    	this.initData  = this.initData;
    	this.doBack = this.doBack;
    }

    /**
	 * 初始化数据
     */
    @action
   	initData = (para={})=>{
        para = {...para,...{requestId:this.requestid}};
        API.getForwardDatas(para).then((reVal)=>{
			this.tableDatas = reVal.tableDatas;
		});
		
		WeaTools.callApi('/api/workflow/label/loadForWardBackLabels', 'GET', {}).then(data => {
            this.doBacknoSelectWarning = data.doBacknoSelectWarning;
        });
   	}

    /**
	 * 收回
     */
   	doBack = (selectRow)=>{

   		if(selectRow.length ==0 ){
            Modal.warning({okText:getLabel(33703,'确认'),title : getLabel(131329,'信息确认'),content: this.doBacknoSelectWarning});
		}else{
   			const thisCxt = this;
            Modal.confirm({
                okText : getLabel(826,'确定'),
                cancelText : getLabel(201,'取消'),
                title : getLabel(131329,'信息确认'),
                content:getLabel(388773,' 您确定要收回选中的转发操作吗？'),
                onOk(){
                    const param = [];
                    const tableDatas = toJS(thisCxt.tableDatas);
                    selectRow.map((index)=>{
                        param.push(tableDatas[index].id);
                    });
                    const para = {selectRow : param,};
                    API.doBack(para).then((reVal)=>{
                        if(reVal.success){
                            message.success(reVal.msg);
                            const { refreshRightMenu, controlDialogVisible,batchControlVariableMap,callbackFun} = thisCxt.callBackActions;
                            typeof refreshRightMenu === 'function' && refreshRightMenu();
                            typeof controlDialogVisible === 'function' && controlDialogVisible(false);
                            typeof callbackFun === 'function' && callbackFun();
                            if(typeof batchControlVariableMap === 'function'){
                                const reloadLogListParams = {pgnumber: 1,atmet: '',maxrequestlogid: 0};
                                batchControlVariableMap({'isRelaodLogList':true,'reloadLogListParams':reloadLogListParams});
                            }
                        }else{
                            message.error(getLabel('388077',"收回转发失败！"));
                        }
                    });
                },
                onCancel(){},
            });
		}
	}
    /**
	 *  转发弹框显隐
     * @param isShow
     */
   	@action
    setShowForwardBack = (isShow,requestid)=>{
		this.showForwardBack = isShow;
		this.requestid = requestid;
        isShow && this.initData();
        const { controlDialogVisible} = this.callBackActions;
        typeof controlDialogVisible === 'function' && controlDialogVisible(false);

    }

	@action
    setCallBackAction = (callBackActions)=>{
		this.callBackActions = callBackActions;
	}


}